home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 August: Tool Chest / Dev.CD Aug 00 TC Disk 2.toast / pc / sample code / interapplication comm / menuscripter / sources / msassubroutines.c < prev    next >
Encoding:
Text File  |  2000-06-23  |  19.5 KB  |  703 lines

  1. // MSASSubroutines.c
  2. //
  3. // Original version by Jon Lansdell and Nigel Humphreys.
  4. // 4.0 and 3.1 updates by Greg Sutton.
  5. // Human Interface changes and GX Printing by Don Swatman
  6. // ©Apple Computer Inc 1996, all rights reserved.
  7.  
  8. #include "MSASSubroutines.h"
  9.  
  10. #include <AppleScript.h>
  11. #include <Resources.h>
  12. #include <AERegistry.h>
  13. #include <ASRegistry.h>
  14. #include <TextUtils.h>
  15.  
  16. #ifdef THINK_C
  17.     #include "PLStrs.h"
  18. #else
  19.     #include <PLStringFuncs.h>
  20. #endif
  21.  
  22. #include "MSAppleEvents.h"
  23. #include "MSAEUtils.h"
  24. #include "MSGlobals.h"
  25. #include "MSWindow.h"
  26.  
  27.  
  28. const short    kSEScriptID = 128;
  29.  
  30. extern ComponentInstance gScriptingComponent;
  31.  
  32. #ifdef THINK_C
  33.     extern pascal StringPtr PLstrcpy(StringPtr str1, StringPtr str2);
  34.     extern pascal StringPtr    PLstrcat(StringPtr str1, StringPtr str2);
  35. #endif
  36.  
  37. AEIdleUPP        gAEIdleUPP;
  38.  
  39. OSAID            gScript1ID;
  40. OSAID            gScript2ID;
  41. // Script 3 targets a script application - its script is not loaded
  42. OSAID            gScript4ID;
  43. OSAID            gScript5ID;
  44.  
  45. // Variables to target script application
  46. ProcessSerialNumber    gScriptAppPSN;
  47. AEDesc                gScriptAppAddress;
  48.  
  49. Str31            gScript1Name = "\pscript shift";
  50. Str31            gScript2Name = "\pscript datestring";
  51. Str31            gScript3Name = "\pscript topseeturvee";
  52. Str31            gScript4Name = "\pscript changecreator";
  53. Str31            gScript5Name = "\pscript get/set selection";
  54.  
  55.  
  56. // Launch an application into the background given an FSSpec
  57.  
  58. OSErr FSSpecLaunchApplication(const FSSpec *fileSpec, ProcessSerialNumber *PSN)
  59. {
  60.     LaunchParamBlockRec launchRec;
  61.     OSErr                err;
  62.  
  63.     launchRec.launchBlockID = extendedBlock;
  64.     launchRec.launchEPBLength = extendedBlockLen;
  65.     launchRec.launchFileFlags = 0;
  66.     launchRec.launchControlFlags = launchContinue + launchNoFileFlags + launchDontSwitch;
  67.     launchRec.launchAppSpec = (FSSpecPtr)fileSpec;
  68.     launchRec.launchAppParameters = nil;
  69.     
  70.     err = LaunchApplication(&launchRec);
  71.  
  72.     if (err == noErr)
  73.         *PSN = launchRec.launchProcessSN;
  74.         
  75.     return(err);
  76. }
  77.  
  78. // Set up the scripts we can send AppleEvents to them.
  79. // Launch the script application.
  80.  
  81. void    SetUpScripts( void )
  82. {
  83.     FSSpec        aSpec;
  84.     OSErr       err;
  85.  
  86.     aSpec.vRefNum = gAppRec.theSpec.vRefNum;
  87.     aSpec.parID = gAppRec.theSpec.parID;
  88.  
  89.     gAEIdleUPP = NewAEIdleProc(IdleProc);    // Universal Procedure - only used in here
  90.  
  91.     PLstrcpy( aSpec.name, gScript1Name );
  92.     (void)LoadScriptFromResFile( &aSpec, kSEScriptID, &gScript1ID );
  93.  
  94.     PLstrcpy( aSpec.name, gScript2Name );
  95.     (void)LoadScriptFromResFile( &aSpec, kSEScriptID, &gScript2ID );
  96.  
  97.     PLstrcpy( aSpec.name, gScript3Name );
  98.     err = FSSpecLaunchApplication( &aSpec, &gScriptAppPSN );
  99.     if (err == noErr) 
  100.         AECreateDesc( typeProcessSerialNumber,(Ptr)&gScriptAppPSN, 
  101.                          sizeof( ProcessSerialNumber ), &gScriptAppAddress );
  102.     else
  103.         gScriptAppAddress.dataHandle = NULL;
  104.  
  105.     PLstrcpy( aSpec.name, gScript4Name );
  106.     (void)LoadScriptFromResFile( &aSpec, kSEScriptID, &gScript4ID );
  107.  
  108.     PLstrcpy( aSpec.name, gScript5Name );
  109.     (void)LoadScriptFromResFile( &aSpec, kSEScriptID, &gScript5ID );
  110.  
  111. }
  112.  
  113. // Given a FileSpec open the resource fork and Load the script resource of resID.
  114. // The resource file will be closed afterwards.
  115.  
  116. OSErr    LoadScriptFromResFile( FSSpec *theSpec, short theResID, OSAID *theScriptID )
  117. {
  118.     short        aFileRef;
  119.     OSErr        err;
  120.  
  121.     *theScriptID = kOSANullScript;
  122.         
  123.     aFileRef = FSpOpenResFile( theSpec, fsRdPerm );
  124.     err = ResError( );
  125.  
  126.     
  127.     if ( noErr == err && aFileRef > 0 )
  128.     {
  129.         err = LoadScriptFromResFileRef( aFileRef, theResID, theScriptID );
  130.         
  131.         CloseResFile( aFileRef );
  132.     }
  133.    
  134.     return err;
  135. }
  136.  
  137. // Given a file reference number for a resource file, and a resource ID
  138. //  for a 'scpt' resource, this routine loads the already compiled script
  139. //  and sets theScriptID to the components reference for it.
  140.  
  141. OSErr    LoadScriptFromResFileRef( short theRefNum, short theResID, OSAID *theScriptID )
  142. {
  143.     short    saveRefNum = CurResFile();    // save current resource
  144.     AEDesc    aScriptDesc = { typeNull, NULL };
  145.     Handle  aHandle;
  146.     OSErr    err;
  147.     
  148.     *theScriptID = kOSANullScript;        // NULL before we do anything
  149.     
  150.     if ( ! theRefNum )
  151.         return errAENoSuchObject;
  152.     
  153.     UseResFile( theRefNum );            // set this resource to be current
  154.  
  155.     aHandle = (Handle)Get1Resource( kOSAScriptResourceType, theResID );
  156.     err = ResError( );
  157.     if ( noErr != err ) goto done;
  158.  
  159.     HLock(aHandle);
  160.     err = AECreateDesc( typeOSAGenericStorage, (Ptr)*aHandle,
  161.                             GetHandleSize( aHandle ), &aScriptDesc );
  162.     HUnlock(aHandle);
  163.     if ( noErr != err ) goto done;
  164.     
  165.     err = OSALoad( gScriptingComponent, &aScriptDesc,
  166.                                       kOSAModeNull, theScriptID );
  167.     
  168. done:
  169.     if ( aHandle )
  170.         ReleaseResource( aHandle );
  171.     UseResFile( saveRefNum );             // reset back to resource previously set
  172.     
  173.     (void)AEDisposeDesc( &aScriptDesc );
  174.     
  175.     return err;
  176. }
  177.  
  178. // Store the scipt ID as a 'scpt' resource, in file given, with the ID given.
  179.  
  180. OSErr    StoreScriptToResFile(FSSpec *theSpec, short theResID, OSAID theScriptID, StringPtr theResName )
  181. {
  182.     short        aFileRef;
  183.     OSErr        err;
  184.     
  185.     if ( theScriptID == kOSANullScript )
  186.         return errAENoSuchObject;
  187.  
  188.     aFileRef = FSpOpenResFile( theSpec, fsWrPerm );
  189.     err = ResError( );
  190.     
  191.     if ( noErr == err && aFileRef > 0 )
  192.     {
  193.         err = StoreScriptToResFileRef( aFileRef, theResID, theScriptID, theResName );
  194.         
  195.         CloseResFile( aFileRef );
  196.     }
  197.     
  198.     return err;
  199. }
  200.  
  201.  
  202. OSErr    StoreScriptToResFileRef( short theFileRef, short theResID, OSAID theScriptID, StringPtr theResName )
  203. {
  204.     short        saveRefNum = CurResFile();    // save current resource file
  205.     AEDesc        scriptData = { typeNull,NULL };
  206.     Handle        aHandle;
  207.     OSErr        err;
  208.     
  209.     if ( kOSANullScript == theScriptID )
  210.         return noErr;    // No script to store so that's okay
  211.     
  212.     if ( ! theFileRef )
  213.         return fnfErr;
  214.     
  215.     UseResFile( theFileRef );        // set this resource to be current
  216.     
  217.     err = (OSErr)OSAStore( gScriptingComponent, theScriptID,
  218.                         typeOSAGenericStorage, kOSAModeNull, &scriptData );
  219.     if ( noErr != err ) goto done;
  220.     
  221.             // Write over current script
  222.     aHandle = Get1Resource( kOSAScriptResourceType, theResID );
  223.     if ( aHandle )
  224.     {
  225.         RemoveResource( aHandle );    // Disposed of in current resource file
  226.         err = ResError( );
  227.         if ( noErr != err ) goto done;
  228.     }
  229.  
  230.     aHandle = scriptData.dataHandle;
  231.     HLock( aHandle );
  232.     HandToHand( &aHandle );                                 
  233.     AddResource( aHandle, typeOSAGenericStorage, theResID, theResName );
  234.     HUnlock( aHandle );
  235.     err = ResError( );
  236.  
  237. done:        
  238.     UpdateResFile( saveRefNum );
  239.  
  240.     (void)AEDisposeDesc( &scriptData );
  241.  
  242.     return err;
  243. }
  244.  
  245. // Quit the script application, store changes to scripts and dispose of scripts.
  246.  
  247. OSErr CleanUpAEScripts(void)
  248. {
  249.     AppleEvent         myAppleEvent = {typeNull,NULL},
  250.                     ignoreReply = {typeNull,NULL};
  251.     FSSpec            fileSpec;
  252.     OSErr            err;
  253.     
  254.                 // Quit the script application we launched
  255.     if (gScriptAppAddress.dataHandle)
  256.     {
  257.         err = AECreateAppleEvent(kCoreEventClass, kAEQuitApplication, &gScriptAppAddress,
  258.                                                                         0, 0, &myAppleEvent);
  259.         
  260.         if (err == noErr)
  261.             err = AESend(&myAppleEvent, &ignoreReply, kAENoReply + kAEAlwaysInteract,
  262.                                     kAENormalPriority, kAEDefaultTimeout, NULL, NULL);
  263.         
  264.         AEDisposeDesc(&myAppleEvent);
  265.     }
  266.  
  267.         // Store the scripts used - this saves any changed properties and aliases
  268.     fileSpec.vRefNum = gAppRec.theSpec.vRefNum;
  269.     fileSpec.parID = gAppRec.theSpec.parID;
  270.  
  271.     PLstrcpy( fileSpec.name, gScript1Name );
  272.     StoreScriptToResFile( &fileSpec, kSEScriptID, gScript1ID, gScript1Name );
  273.  
  274.     PLstrcpy( fileSpec.name, gScript2Name );
  275.     StoreScriptToResFile( &fileSpec, kSEScriptID, gScript2ID, gScript2Name );
  276.  
  277.     PLstrcpy( fileSpec.name, gScript4Name );
  278.     StoreScriptToResFile( &fileSpec, kSEScriptID, gScript4ID, gScript4Name );
  279.  
  280.     PLstrcpy( fileSpec.name, gScript5Name );
  281.     StoreScriptToResFile( &fileSpec, kSEScriptID, gScript5ID, gScript5Name );
  282.  
  283.     (void)OSADispose( gScriptingComponent, gScript1ID );
  284.     (void)OSADispose( gScriptingComponent, gScript2ID );
  285.     (void)OSADispose( gScriptingComponent, gScript4ID );
  286.     (void)OSADispose( gScriptingComponent, gScript5ID );
  287.     
  288.     return(err);
  289. }
  290.  
  291.  
  292. //    Calls a script subroutine that takes two window names or numbers.
  293. //    If a window doesn't exist then it is created, one window is then
  294. //    moved below the other.
  295. //    Uses predefined label parameters.
  296. //        on shift around winMove below winStay
  297.  
  298. OSErr ExecuteScript1(DPtr theDoc)
  299. {
  300. #ifdef __MWERKS__
  301.     #pragma unused (theDoc)
  302. #endif
  303.  
  304.     AppleEvent         myAppleEvent = {typeNull,NULL},
  305.                     ignoreReply = {typeNull,NULL};
  306.     AEDesc            selfAddress;
  307.     OSErr           myErr;
  308.     Str255          handlerName = "\pshift",
  309.                     below = "\pBelow",
  310.                     above = "\pAbove";
  311.     
  312.     if (gScript1ID == kOSANullScript)
  313.         return(noErr);
  314.     
  315.     myErr = MakeSelfAddress(&selfAddress);
  316.     if (myErr != noErr) return noErr; 
  317.  
  318.             // Set up a subroutine AppleEvent
  319.     myErr = AECreateAppleEvent(kASAppleScriptSuite, kASSubroutineEvent, &selfAddress,
  320.                                                                  kAutoGenerateReturnID, kAnyTransactionID, &myAppleEvent);
  321.    
  322.            // Add the name of the subroutine
  323.     if (myErr == noErr)
  324.            myErr = AEPutParamPtr(&myAppleEvent, keyASSubroutineName, typeChar,    
  325.                                                                    (Ptr)&handlerName[1], handlerName[0]);
  326.            
  327.            // Add the first predefined label parameter                                                
  328.     if (myErr == noErr)
  329.         myErr = AEPutParamPtr(&myAppleEvent, keyASPrepositionAround, typeChar,    
  330.                                                                 (Ptr)&below[1], below[0]);
  331.                                                                        
  332.            // Add the second predefined label parameter                                                
  333.     if (myErr == noErr)
  334.         myErr = AEPutParamPtr(&myAppleEvent, keyASPrepositionBelow, typeChar,    
  335.                                                                 (Ptr)&above[1], above[0]);
  336.         
  337.     if (myErr == noErr)
  338.         myErr = OSADoEvent(gScriptingComponent, &myAppleEvent, gScript1ID,
  339.                                         kOSAModeAlwaysInteract, &ignoreReply);
  340.                                         
  341.       (void)AEDisposeDesc( &selfAddress );
  342.     (void)AEDisposeDesc( &myAppleEvent );
  343.     (void)AEDisposeDesc( &ignoreReply );
  344.     
  345.     return(myErr);
  346. }
  347.     
  348. //    Calls a subroutine that returns the date and/or time depending
  349. //    on parameters sent.
  350. //    Uses subroutine defined label parameters.
  351. //        on datestring given wDate:fDate, wTime:fTime
  352.  
  353. OSErr ExecuteScript2(DPtr theDoc)
  354. {
  355. #ifdef __MWERKS__
  356.     #pragma unused (theDoc)
  357. #endif
  358.  
  359.     AppleEvent         myAppleEvent = {typeNull,NULL},
  360.                     myReply = {typeNull,NULL};        // Need to NULL for OSADoEvent() routine
  361.     AEDesc            selfAddress,
  362.                     textDesc;
  363.     AEDescList        paramList = {typeNull,NULL};
  364.     OSErr           myErr;
  365.     Str255          pStr = "\pdatestring";
  366.     
  367.     if (gScript2ID == kOSANullScript)
  368.         return(noErr);
  369.  
  370.     myErr = MakeSelfAddress(&selfAddress);
  371.     if (myErr != noErr) return noErr;
  372.  
  373.                 // Create an AppleScript subroutine AppleEvent
  374.     myErr = AECreateAppleEvent(kASAppleScriptSuite, kASSubroutineEvent, &selfAddress,
  375.                                                                  kAutoGenerateReturnID, kAnyTransactionID, &myAppleEvent);
  376.    
  377.                // Specify the routine by it's name
  378.     if (myErr == noErr)
  379.            myErr = AEPutParamPtr(&myAppleEvent, keyASSubroutineName, typeChar,    
  380.                                                                    (Ptr)&pStr[1], pStr[0]);
  381.  
  382.             // Create list for user defined label parameters
  383.     if (myErr == noErr)
  384.         myErr = AECreateList(NULL, 0 , false, ¶mList);
  385.  
  386.     if (myErr == noErr)
  387.     {            // Make sure label name is in lower case
  388.         PLstrcpy(pStr,"\pwdate");
  389.         myErr = AEPutPtr(¶mList, 0 ,typeChar ,(Ptr)&pStr[1],  pStr[0]);
  390.         if (myErr == noErr) myErr = AEPutPtr(¶mList, 0, typeTrue , NULL,  0);
  391.     }
  392.  
  393.     if (myErr == noErr)
  394.     {
  395.         PLstrcpy(pStr,"\pwtime");
  396.         myErr = AEPutPtr(¶mList, 0 ,typeChar ,(Ptr)&pStr[1],  pStr[0]);
  397.         if (myErr == noErr)  AEPutPtr(¶mList, 0, typeFalse , NULL,  0);
  398.     }
  399.  
  400.             // Add list to AppleEvent
  401.     if (myErr == noErr)
  402.         myErr = AEPutParamDesc(&myAppleEvent, keyASUserRecordFields, ¶mList);
  403.  
  404.     if (myErr == noErr)
  405.         myErr = OSADoEvent(gScriptingComponent, &myAppleEvent, gScript2ID,
  406.                                                     kOSAModeAlwaysInteract, &myReply);
  407.       
  408.       AEDisposeDesc(&selfAddress);
  409.     AEDisposeDesc(&myAppleEvent);
  410.     AEDisposeDesc(¶mList);
  411.     
  412.     if (myErr == noErr)
  413.         myErr = GetTextDescFromReply(&myReply, &textDesc);
  414.  
  415.     if (myErr == noErr)
  416.            myErr = SetSelection(&textDesc);
  417.         
  418.     AEDisposeDesc(&textDesc);
  419.     AEDisposeDesc(&myReply);
  420.         
  421.     return(myErr);
  422. }
  423.  
  424. //    First of all gets the current selection. Then calls a script application
  425. //    subroutine to twist the text in the selection. Finally it sets the selection
  426. //    with the result.
  427. //    Uses a positional parameter.
  428. //        on topseeturvee(someText)
  429.  
  430. OSErr ExecuteScript3(DPtr theDoc)
  431. {
  432. #ifdef __MWERKS__
  433.     #pragma unused (theDoc)
  434. #endif
  435.  
  436.     AppleEvent         myAppleEvent = {typeNull,NULL},
  437.                     myReply = {typeNull,NULL};
  438.     AEDescList       paramList = {typeNull,NULL},
  439.                     textDesc;
  440.     OSErr           myErr;
  441.     Str255          pStr = "\ptopseeturvee";
  442.     
  443.     if (! gScriptAppAddress.dataHandle)
  444.         return(noErr);
  445.         
  446.         // Get the current selection of the front window
  447.     myErr = GetSelection(&textDesc);
  448.     
  449.         // Create a subroutine AppleEvent
  450.     if (myErr == noErr)
  451.         myErr = AECreateAppleEvent(kASAppleScriptSuite, kASSubroutineEvent, &gScriptAppAddress,
  452.                                                                  kAutoGenerateReturnID, kAnyTransactionID, &myAppleEvent);
  453.    
  454.            // Add the name of the subroutine
  455.     if (myErr == noErr)
  456.            myErr = AEPutParamPtr(&myAppleEvent, keyASSubroutineName, typeChar,    
  457.                                                                    (Ptr)&pStr[1], pStr[0]);
  458.  
  459.         // Create positional parameter list
  460.     if (myErr == noErr)
  461.         myErr = AECreateList(NULL, 0 ,false, ¶mList);
  462.         
  463.         // Add textDesc as positional parameter
  464.     if (myErr == noErr)
  465.         myErr = AEPutDesc(¶mList, 0, &textDesc);
  466.  
  467.         // Add positional parameter list
  468.     if (myErr == noErr)
  469.         myErr = AEPutParamDesc(&myAppleEvent, keyDirectObject, ¶mList);
  470.         
  471.     if (myErr == noErr)
  472.         myErr = AESend(&myAppleEvent, &myReply, kAEWaitReply + kAEAlwaysInteract,
  473.                                     kAENormalPriority, kAEDefaultTimeout, gAEIdleUPP, NULL);
  474.       
  475.     AEDisposeDesc(&myAppleEvent);
  476.     AEDisposeDesc(¶mList);
  477.         // Dispose of textDesc before we get one in reply
  478.     AEDisposeDesc(&textDesc);
  479.     
  480.     if (myErr == noErr)
  481.         myErr = GetTextDescFromReply(&myReply, &textDesc);
  482.  
  483.     if (myErr == noErr)
  484.     {
  485.            myErr = SetSelection(&textDesc);
  486.            AEDisposeDesc(&textDesc);
  487.        }
  488.         
  489.     AEDisposeDesc(&myReply);
  490.         
  491.     return(myErr);
  492. }
  493.  
  494. //    Calls script subroutine that changes a given file's creator
  495. //    to the creator specified.
  496. //    Uses direct and predefined label parameters.
  497. //        on changecreator of aSpec into aCreator
  498.  
  499. OSErr ExecuteScript4(DPtr theDoc)
  500. {
  501.     AppleEvent         myAppleEvent = {typeNull,NULL},
  502.                     ignoreReply = {typeNull,NULL};
  503.     AEDesc            selfAddress;
  504.     AEDescList       paramList = {typeNull,NULL};
  505.     OSErr           myErr;
  506.     Str255          pStr = "\pchangecreator";
  507.     
  508.     if (gScript4ID == kOSANullScript)
  509.         return(noErr);
  510.  
  511.     myErr = MakeSelfAddress(&selfAddress);
  512.     if (myErr != noErr) return myErr;
  513.         
  514.         // Create a subroutine AppleEvent
  515.     myErr = AECreateAppleEvent(kASAppleScriptSuite, kASSubroutineEvent, &selfAddress,
  516.                                                 kAutoGenerateReturnID, kAnyTransactionID, &myAppleEvent);
  517.    
  518.            // Add the name of the subroutine
  519.     if (myErr == noErr)
  520.            myErr = AEPutParamPtr(&myAppleEvent, keyASSubroutineName, typeChar,    
  521.                                                    (Ptr)&pStr[1], pStr[0]);
  522.            
  523.            // Add the direct parameter                                                
  524.     if (myErr == noErr)
  525.            myErr = AEPutParamPtr(&myAppleEvent, keyDirectObject, typeFSS,    
  526.                                                 (Ptr)&(theDoc->theFSSpec), sizeof(FSSpec));
  527.  
  528.            // Add the subroutine parameter label                                            
  529.     if (myErr == noErr)
  530.     {
  531.         PLstrcpy(pStr, "\pToyS");
  532.            myErr = AEPutParamPtr(&myAppleEvent, keyASPrepositionInto, typeChar,    
  533.                                                         (Ptr)&pStr[1], pStr[0]);
  534.     }
  535.  
  536.         
  537.     if (myErr == noErr)
  538.         myErr = OSADoEvent(gScriptingComponent, &myAppleEvent, gScript4ID,
  539.                                                 kOSAModeAlwaysInteract, &ignoreReply);
  540.       
  541.       AEDisposeDesc(&selfAddress);
  542.     AEDisposeDesc(&myAppleEvent);
  543.     AEDisposeDesc(&ignoreReply);
  544.     
  545.     return(myErr);
  546. }
  547.  
  548.  
  549. //    Calls script subroutine to get this application's selection.
  550. //    Uses no parameters except for subroutine name.
  551. //        on getselection()
  552.  
  553. OSErr    GetSelection(AEDesc *textDesc)
  554. {
  555.     AppleEvent         myAppleEvent = {typeNull,NULL},
  556.                     myReply = {typeNull,NULL};
  557.     AEDesc            selfAddress;
  558.     OSErr           myErr;
  559.     Str255          pStr = "\pgetselection";
  560.     
  561.     if (gScript5ID == kOSANullScript)
  562.         return(noErr);
  563.  
  564.     myErr = MakeSelfAddress(&selfAddress);
  565.     if (myErr != noErr) return myErr;
  566.         
  567.                 // Create an AppleScript subroutine AppleEvent
  568.     myErr = AECreateAppleEvent(kASAppleScriptSuite, kASSubroutineEvent, &selfAddress,
  569.                                                                  kAutoGenerateReturnID, kAnyTransactionID, &myAppleEvent);
  570.    
  571.                // Specify the routine by it's name
  572.     if (myErr == noErr)
  573.            myErr = AEPutParamPtr(&myAppleEvent, keyASSubroutineName, typeChar,    
  574.                                                                    (Ptr)&pStr[1], pStr[0]);
  575.  
  576.     if (myErr == noErr)
  577.         myErr = OSADoEvent(gScriptingComponent, &myAppleEvent, gScript5ID,
  578.                                                     kOSAModeAlwaysInteract, &myReply);
  579.  
  580.     myErr = GetTextDescFromReply(&myReply, textDesc);
  581.     
  582.       AEDisposeDesc(&selfAddress);
  583.     AEDisposeDesc(&myAppleEvent);
  584.     AEDisposeDesc(&myReply);
  585.         
  586.     return(myErr);
  587. }
  588.  
  589.  
  590. //    Calls script subroutine to set this application's selection.
  591. //    Uses a positional parameter.
  592. //        setselection(aString)
  593.  
  594. OSErr    SetSelection(AEDesc *textDesc)
  595. {
  596.     AppleEvent         myAppleEvent = {typeNull,NULL},
  597.                     myReply = {typeNull,NULL};
  598.     AEDesc            selfAddress;
  599.     AEDescList       paramList = {typeNull,NULL};
  600.     OSErr           myErr;
  601.     Str255          pStr = "\psetselection";
  602.     
  603.     if (gScript5ID == kOSANullScript)
  604.         return(noErr);
  605.  
  606.     myErr = MakeSelfAddress(&selfAddress);
  607.     if (myErr != noErr) return myErr;
  608.     
  609.             // Create an AppleScript subroutine AppleEvent
  610.     myErr = AECreateAppleEvent(kASAppleScriptSuite, kASSubroutineEvent, &selfAddress,
  611.                                                                  kAutoGenerateReturnID, kAnyTransactionID, &myAppleEvent);
  612.    
  613.                // Specify the routine by it's name
  614.     if (myErr == noErr)
  615.            myErr = AEPutParamPtr(&myAppleEvent, keyASSubroutineName, typeChar,    
  616.                                                                    (Ptr)&pStr[1], pStr[0]);
  617.  
  618.             // Create positional parameter list
  619.     if (myErr == noErr)
  620.         myErr = AECreateList(NULL, 0 ,false, ¶mList);
  621.         
  622.             // Add textDesc as positional parameter
  623.     if (myErr == noErr)
  624.         myErr = AEPutDesc(¶mList, 0, textDesc);
  625.  
  626.             // Add positional parameter list
  627.     if (myErr == noErr)
  628.         myErr = AEPutParamDesc(&myAppleEvent, keyDirectObject, ¶mList);
  629.  
  630.     if (myErr == noErr)
  631.         myErr = OSADoEvent(gScriptingComponent, &myAppleEvent, gScript5ID,
  632.                                                     kOSAModeAlwaysInteract, &myReply);
  633.     
  634.       AEDisposeDesc(&selfAddress);
  635.     AEDisposeDesc(&myAppleEvent);
  636.     AEDisposeDesc(¶mList);
  637.     AEDisposeDesc(&myReply);
  638.         
  639.     return(myErr);
  640. }
  641.  
  642. // Takes a reply and tries to get a text descriptor from it.
  643.  
  644. OSErr    GetTextDescFromReply(AEDesc *aReply, AEDesc *textDesc)
  645. {
  646.     OSErr        myErr;
  647.  
  648.     textDesc->descriptorType = typeNull;
  649.     textDesc->dataHandle = NULL;
  650.  
  651.     myErr = AEGetParamDesc(aReply, keyAEResult, typeChar, textDesc);
  652.     
  653.     return(myErr);
  654. }
  655.  
  656.  
  657. void    EnableAEScriptItems(Boolean fEnable)
  658. {
  659.     SetMenuItemState ( (fEnable && kOSANullScript != gScript1ID), myMenus[subroutineM], cScript1);
  660.     SetMenuItemState ( (fEnable && kOSANullScript != gScript2ID), myMenus[subroutineM], cScript2);
  661.     SetMenuItemState ( (fEnable && gScriptAppAddress.dataHandle), myMenus[subroutineM], cScript3);
  662.     SetMenuItemState ( (fEnable && kOSANullScript != gScript4ID), myMenus[subroutineM], cScript4);
  663. }
  664.  
  665.     // IdleProc for AESend 
  666. pascal Boolean IdleProc(EventRecord *myEvent, long *sleep, RgnHandle *mouseRgn)
  667. {
  668.     WindowPtr   theWindow;
  669.     Boolean     activate;
  670.  
  671.     switch ( myEvent->what )
  672.     {
  673.         case nullEvent:
  674.             *sleep = 0;                         // no null processing in this sample
  675.             mouseRgn = nil;
  676.             break;
  677.                 
  678.         case activateEvt:
  679.             activate = ((myEvent->modifiers & activeFlag) != 0);
  680.             theWindow = (WindowPtr)myEvent->message;
  681.             DoActivate(theWindow, activate);
  682.             break;
  683.  
  684.         case updateEvt:
  685.             DoUpdate( (WindowPtr)myEvent->message );
  686.             break;
  687.  
  688.         case osEvt:
  689.             if ( ( myEvent->message >> 24 ) & suspendResumeMessage )    // suspend or resume
  690.             {
  691.                 gInBackground = ((myEvent->message & resumeFlag) == 0);
  692.                 DoActivate(FrontWindow(), ! gInBackground);
  693.             }
  694.             break;
  695.             
  696.         default:
  697.             SysBeep( 1 );
  698.             break;
  699.     }
  700.     
  701.     return false;    // I'll wait forever
  702. }
  703.